﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Xml;
using System.Xml.XPath;
using System.Text.RegularExpressions;

namespace FTI.SP.LADM.EnhancedContentQueryWebPart.Exslt
{
    public sealed class ExsltStrings
    {
        // Fields
        private static ExsltStrings _Instance;

        // Methods
        private ExsltStrings()
        {
        }

        public static string Concat(XPathNodeIterator nodeSet)
        {
            StringBuilder builder = new StringBuilder();
            while (nodeSet.MoveNext())
            {
                builder.Append(nodeSet.Current.Value);
            }
            return builder.ToString();
        }

        public static string Lowercase(string str)
        {
            return str.ToLower(CultureInfo.CurrentCulture);
        }

        public static string Padding(int number)
        {
            string str = string.Empty;
            if (number < 0)
            {
                return str;
            }
            return str.PadLeft(number);
        }

        public static string Padding(int number, string str)
        {
            if (number < 0)
            {
                return string.Empty;
            }
            StringBuilder builder = new StringBuilder(str);
            while (builder.Length < number)
            {
                builder.Append(str);
            }
            if (builder.Length > number)
            {
                return builder.Remove(number, builder.Length - number).ToString();
            }
            return builder.ToString();
        }

        public static string Replace(string str, string oldValue, string newValue)
        {
            return str.Replace(oldValue, newValue);
        }

        public static XPathNodeIterator Split(string str)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml("<tokens/>");
            foreach (string str2 in str.Split(new char[] { ' ' }))
            {
                if (!string.IsNullOrEmpty(str2))
                {
                    XmlElement newChild = document.CreateElement("token");
                    newChild.InnerText = str2;
                    document.DocumentElement.AppendChild(newChild);
                }
            }
            return document.CreateNavigator().Select("//token");
        }

        public static XPathNodeIterator Split(string str, string delimiter)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml("<tokens/>");
            if (string.IsNullOrEmpty(delimiter))
            {
                foreach (char ch in str)
                {
                    XmlElement newChild = document.CreateElement("token");
                    newChild.InnerText = ch.ToString();
                    document.DocumentElement.AppendChild(newChild);
                }
            }
            else
            {
                delimiter = delimiter.Replace(@"\", @"\\").Replace("$", @"\$").Replace("^", @"\^");
                delimiter = delimiter.Replace("{", @"\{").Replace("[", @"\[").Replace("(", @"\(");
                delimiter = delimiter.Replace("*", @"\*").Replace(")", @"\)").Replace("|", @"\|");
                delimiter = delimiter.Replace("+", @"\+").Replace("?", @"\?").Replace(".", @"\.");
                Regex regex = new Regex(delimiter);
                foreach (string str2 in regex.Split(str))
                {
                    if (!string.IsNullOrEmpty(str2) && (string.Compare(str2, delimiter, false, CultureInfo.CurrentCulture) != 0))
                    {
                        XmlElement element2 = document.CreateElement("token");
                        element2.InnerText = str2;
                        document.DocumentElement.AppendChild(element2);
                    }
                }
            }
            return document.CreateNavigator().Select("//token");
        }

        public static XPathNodeIterator Tokenize(string str)
        {
            Regex regex = new Regex(@"\s+");
            XmlDocument document = new XmlDocument();
            document.LoadXml("<tokens/>");
            foreach (string str2 in regex.Split(str))
            {
                XmlElement newChild = document.CreateElement("token");
                newChild.InnerText = str2;
                document.DocumentElement.AppendChild(newChild);
            }
            return document.CreateNavigator().Select("//token");
        }

        public static XPathNodeIterator Tokenize(string str, string delimiters)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml("<tokens/>");
            foreach (string str2 in str.Split(delimiters.ToCharArray()))
            {
                XmlElement newChild = document.CreateElement("token");
                newChild.InnerText = str2;
                document.DocumentElement.AppendChild(newChild);
            }
            return document.CreateNavigator().Select("//token");
        }

        public static string Uppercase(string str)
        {
            return str.ToUpper(CultureInfo.CurrentCulture);
        }

        // Properties
        public static ExsltStrings Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new ExsltStrings();
                }
                return _Instance;
            }
        }

    }
}
